package test.client;

import java.util.LinkedList;
import java.util.List;
import org.je.util.StringUtil;
import com.fyf.game.net.ReceiveHandler;
import com.fyf.game.net.Sender;
import com.fyf.game.net.business.Cmd;

public class ChartReceiveHandler implements ReceiveHandler
{
	public void onReceive(String msg, Sender sender) {
		List<String> li = StringUtil.split(msg ,Cmd.spr);
		li = StringUtil.trimAll(StringUtil.deleteBlankOrNull(li));

		if (msg.startsWith(Cmd.chart)) {

			String name = li.get(1);
			String m = li.get(2);

			int c = m.indexOf("#");
			String[] arr = new String[] { name, m.substring(0 ,c).trim(),
					m.substring(c + 1).trim() };

			LinkedList<String[]> msgs = UIShared.chartList;
			synchronized (msgs) {
				if (msgs.size() > 20) {
					msgs.removeFirst();
				}
				msgs.add(arr);
			}
		} else if (msg.startsWith(Cmd.login)) {
			if (li.get(1).equals(Cmd.ok)) {
				UIShared.message = li.get(3) + " , 登录成功! ";
				UIShared.userArea.addUser(li.get(2) ,li.get(3));
				UIShared.userArea.updateText();
				UIShared.chartUI.scanUsers();
			} else if (li.get(2).equals(Cmd.id_is_empty)) {
				UIShared.message = "登录失败! ID不能为空.";
			} else {
				UIShared.message = "登录失败! ID[" + li.get(2) + "]已经登录.";
			}
		} else if (msg.startsWith(Cmd.logout)) {
			if (li.get(1).equals(Cmd.ok)) {
				UIShared.message = li.get(3) + " , 退出成功!";
				UIShared.userArea.removeUser(li.get(2));
				UIShared.userArea.updateText();
			} else {
				UIShared.message = li.get(3) + " , 退出失败!";
			}
		} else if (msg.startsWith(Cmd.scan_user)) {
			if (li.size() > 1) {
				List<String> lines = StringUtil.split(li.get(1) ,"#");
				for (String line : lines) {
					int c = line.indexOf("@");
					UIShared.userArea.addUser(line.substring(0 ,c).trim() ,line
							.substring(c + 1).trim());
				}
				UIShared.userArea.updateText();
			}
		}

		UIShared.showArea.updateText();
	}
}
